define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatsea/index' + location.search,
                    info_url: 'business/info/index',
                    add_url: 'business/privatsea/add',
                    edit_url: 'business/privatsea/edit',
                    del_url: 'business/privatsea/del',
                    recovery_url: 'business/privatesea/recovery',
                    multi_url: 'business/privatsea/multi',
                    import_url: 'business/privatsea/import',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'gender', title: __('Gender'), searchList: { "0": __('Gender 0'), "1": __('Gender 1'), "2": __('Gender 2') }, formatter: Table.api.formatter.normal },
                        { field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'source.name', title: __('Sourceid') },
                        { field: 'provinces.name', title: __('Province'), operate: 'LIKE' },
                        { field: 'citys.name', title: __('City'), operate: 'LIKE' },
                        { field: 'districts.name', title: __('District'), operate: 'LIKE' },
                        { field: 'auth', title: __('Auth'), searchList: { "0": __('Auth 0'), "1": __('Auth 1') }, formatter: Table.api.formatter.normal },
                        { field: 'money', title: __('Money'), operate: 'BETWEEN' },
                        { field: 'deal', title: __('Deal'), searchList: { "0": __('Deal 0'), "1": __('Deal 1') }, formatter: Table.api.formatter.normal },
                        { field: 'poster', title: __('Poster'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'parentid', title: __('Parentid') },
                        { field: 'admin.nickname', title: __('Adminid') },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: '客户详情',
                                    classname: "btn btn-xs btn-success btn-dialog",
                                    icon: 'fa fa-eye',
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                },
                                {
                                    name: 'recovery',
                                    title: '客户回收',
                                    icon: 'fa fa-recycle',
                                    extend: 'data-toggle="tooltip"',
                                    confirm: '是否确认回收客户？',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.recovery_url,
                                    success: () => $(".btn-refresh").trigger("click")
                                },
                            ]
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


        },

        add: function () {
            Controller.api.GetRegion();

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.GetRegion();

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            GetRegion: function () {
                $("#region").on("cp:updated", function () {
                    var citypicker = $(this).data("citypicker");
                    var province = citypicker.getCode("province");
                    var city = citypicker.getCode("city");
                    var district = citypicker.getCode("district");
                    if (province) {
                        $("input[name='row[province]']").val(province)
                    }

                    if (city) {
                        $("input[name='row[city]']").val(city)
                    }

                    if (district) {
                        $("input[name='row[district]']").val(district)
                    }
                })
            },
        }
    };
    return Controller;
});
